# Maintainer: Carsten Haitzler <raster@rasterman.com>
# Contributor: Ryan Gammon <ryan@gamnation.net>

_pkgname=efl
pkgbase=mingw-w64-${_pkgname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_pkgname}"
pkgver=1.25.0.r672.gd9ec36e1de
pkgrel=1
pkgdesc="Enlightenment Foundation Libraries - Development version"
arch=('any')
mingw_arch=('mingw32' 'mingw64')
url="https://www.enlightenment.org"
license=('BSD' 'LGPL2.1' 'GPL2' 'MIT' 'custom')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs" "${MINGW_PACKAGE_PREFIX}-openjpeg2"
         "${MINGW_PACKAGE_PREFIX}-lz4" "${MINGW_PACKAGE_PREFIX}-luajit" 
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo" "${MINGW_PACKAGE_PREFIX}-dbus"
         "${MINGW_PACKAGE_PREFIX}-libsndfile" "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libtiff" "${MINGW_PACKAGE_PREFIX}-libavif"
         "${MINGW_PACKAGE_PREFIX}-libheif" "${MINGW_PACKAGE_PREFIX}-libjxl"
         "${MINGW_PACKAGE_PREFIX}-freetype" "${MINGW_PACKAGE_PREFIX}-fontconfig"
         "${MINGW_PACKAGE_PREFIX}-fribidi" "${MINGW_PACKAGE_PREFIX}-pixman"
         "${MINGW_PACKAGE_PREFIX}-gstreamer" "${MINGW_PACKAGE_PREFIX}-gst-plugins-base"
         "${MINGW_PACKAGE_PREFIX}-libraw" "${MINGW_PACKAGE_PREFIX}-openssl")
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-mono"
  "${MINGW_PACKAGE_PREFIX}-meson"
  "${MINGW_PACKAGE_PREFIX}-pkgconf"
  "git"
)
_commit="d9ec36e1de4c2a70ac82dc66a72c282dc42037b7"
source=("git+https://git.enlightenment.org/enlightenment/efl.git#commit=${_commit}"
        "fix-prefix.patch"
        "winmono.patch")
sha256sums=('SKIP'
            '9ec7c0a0587a075133ef0d4cb986e9d26970a77cc42d23dfa5ed466d4b851bd5'
            'b89925237f5f47b04b44f4328a35263d2c5292727518d2f367abc919482a853f')

pkgver() {
  cd "$_pkgname"

  git describe --long "${_commit}" | sed 's/\([^-]*-g\)/r\1/;s/-/./g;s/^v//g'
}

prepare() {
  cd "$_pkgname"

  patch -Np1 -i "${srcdir}/fix-prefix.patch"
  patch -Np1 -i "${srcdir}/winmono.patch"

  # Work around https://github.com/mesonbuild/meson/issues/7267 by creating mcs.bat based on
  # https://github.com/mono/release/blob/master/windows-installer/bat/wrapper.bat
  # and using that as the mono compiler since meson+python subprocess+windows can't run shebang 
  # shell scripts like /mingw64/bin/mcs. Efl supports -Ddotnet as well which doesn't have this 
  # issue, but .net core is not part of mingw and needs to be installed in windows.
  echo @`cygpath -w ${MINGW_PREFIX}/bin/mono.exe` --gc-params=nursery-size=64m %MONO_OPTIONS% `cygpath -w ${MINGW_PREFIX}/lib/mono/4.5/csc.exe` %* > ./csc.bat
}

build() {
  mkdir -p "build-${MSYSTEM}" && cd "build-${MSYSTEM}"

  # EFL_RUN_IN_TREE needs to be set manually on Windows. See:
  # https://github.com/Enlightenment/efl-ci/commit/18ce3e8066f954bd32f2ba890e308def86c6a668
  export EFL_RUN_IN_TREE=1

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  CSC="${srcdir}/${_pkgname}/csc.bat" \
    meson setup \
      --prefix="${MINGW_PREFIX}" \
      --default-library shared \
      -Dsystemd=false \
      -Dpulseaudio=false \
      -Dv4l2=false \
      -Dlibmount=false \
      -Deeze=false \
      -Dx11=false \
      -Dxinput2=false \
      -Devas-loaders-disabler='pdf','ps','rsvg','json' \
      -Dopengl=none \
      -Dpixman=true \
      -Dembedded-lz4=false \
      -Dfribidi=true \
      -Dinput=false \
      -Dbuild-examples=false \
      -Dbuild-tests=false \
      -Dbindings=mono \
      -Dbuild-examples=false \
      -Dbuild-tests=false \
      "../${_pkgname}"

   meson compile
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="$pkgdir" meson install

  cd "${srcdir}/${_pkgname}"
  install -Dm644 -t "${pkgdir}${MINGW_PREFIX}/share/doc/$_pkgname/" README.md
  install -Dm644 -t "${pkgdir}${MINGW_PREFIX}/share/licenses/$pkgname/" AUTHORS COMPLIANCE COPYING
}
